package com.liucanwen.citylist.hccitylist.view;

import android.annotation.TargetApi;
import android.content.Context;
import android.os.Build;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.WindowManager;
import android.widget.LinearLayout;

/**
 * Created by Administrator on 2016/9/1.
 */
public class TouchView extends LinearLayout {
//    private int mScreenHeight ;
    private int mPerHeight ;
    public TouchView(Context context) {
        super(context);
        init();
    }

    public TouchView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public TouchView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    public TouchView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        init();
    }

    private void init(){
//        WindowManager wm = (WindowManager) getContext()
//                .getSystemService(Context.WINDOW_SERVICE);
//        mScreenHeight = wm.getDefaultDisplay().getHeight();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
//        Log.e("positionAA","  height  "+heightMeasureSpec);
//        Log.e("positionAA","  getHeight  "+getHeight());
        if ( mPerHeight==0 && getMeasuredHeight()!=0 ){
            mPerHeight = getMeasuredHeight()/23 ;
            Log.e("positionAA"," mPerHeight  "+mPerHeight + "  getMeasuredHeight() "+ getMeasuredHeight());
        }
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    private OnSelectedPositionListener mListener ;
    public void setOnSelectedPositionListener(OnSelectedPositionListener listener){
        mListener = listener ;
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        int y = (int) event.getY();
        Log.e("positionAA"," y  "+y);
        if ( event.getAction() == MotionEvent.ACTION_UP)
            mListener.onTouchUp();
        if ( event.getAction() == MotionEvent.ACTION_DOWN)
            mListener.onTouchDwon();
        mListener.onSelected(y/mPerHeight);
        return true;
    }

    public interface OnSelectedPositionListener{
        public void onSelected(int position);
        public void onTouchUp();
        public void onTouchDwon();
    }

}
